﻿@page "/identityprovisioning"
@inherits Fluxor.Blazor.Web.Components.FluxorComponent
@inject IState<SearchIdentityProvisioningState> searchIdentityProvisioningState
@inject IDispatcher dispatcher
@inject NotificationService notificationService
@inject DialogService dialogService
@inject ContextMenuService contextMenuService
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.IdentityProvisioningStore;
@using SimpleIdServer.IdServer.Website.Stores.RealmStore;

<RadzenText class="mb-3 no-margin" Text="Extract" TextStyle="TextStyle.DisplayH3" />
<p>
    @Global.IdentityProvisioningLstDescription
</p>

<RadzenDataGrid @ref="identityProvisioningGrid"
                AllowFiltering="true"
                AllowColumnResize="true"
                AllowAlternatingRows="false"
                FilterMode="FilterMode.Simple"
                FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive"
                AllowSorting="true"
                PageSize="15"
                AllowPaging="true"
                PagerHorizontalAlign="HorizontalAlign.Left"
                ShowPagingSummary="true"
                IsLoading="@searchIdentityProvisioningState.Value.IsLoading"
                Count="@searchIdentityProvisioningState.Value.Count"
                Data="@searchIdentityProvisioningState.Value.Values"
                LoadData="@LoadIdentityProvisioningLst"
                RowRender="@RowRender"
                TItem="SelectableIdentityProvisioning"
                ColumnWidth="300px">
    <Columns>
        <RadzenDataGridColumn TItem="SelectableIdentityProvisioning" Filterable="false" Sortable="false" Title="@Global.Name" Width="80px">
            <Template Context="data">                
                <RadzenLink Text="@data.Value.Name" Path="@($"/identityprovisioning/{data.Value.Id}/details")" />
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="SelectableIdentityProvisioning" Property="Value.Description" Filterable="false" Sortable="false" Title="@Global.Description" Width="80px" />
        <RadzenDataGridColumn TItem="SelectableIdentityProvisioning" Filterable="false" Sortable="false" Title="@Global.Active" Width="80px">
            <Template Context="data">
                @if(data.Value.IsEnabled)
                {
                    <RadzenBadge Text="@Global.Active" IsPill="true" BadgeStyle="BadgeStyle.Success"></RadzenBadge>
                }
                else
                {
                    <RadzenBadge Text="@Global.Disable" IsPill="true" BadgeStyle="BadgeStyle.Danger"></RadzenBadge>
                }
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="SelectableIdentityProvisioning" Property="Value.UpdateDateTime" Filterable="false" Sortable="true" FormatString="{0:dd/M/yyyy HH:mm:ss}" SortOrder="SortOrder.Descending" Title="@Global.UpdateDateTime" Width="80px" />
    </Columns>
</RadzenDataGrid>


@code {
    RadzenDataGrid<SelectableIdentityProvisioning> identityProvisioningGrid;
    bool selectAll;

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if(firstRender)
        {
            SubscribeToAction<SelectRealmSuccessAction>((act) =>
            {
                identityProvisioningGrid.Reload();
                StateHasChanged();
            });
            if (!searchIdentityProvisioningState.Value.IsLoading)
                identityProvisioningGrid.Reload();
        }
    }

    void RowRender(RowRenderEventArgs<SelectableIdentityProvisioning> row)
    {
        const string className = "class";
        if (row.Data.IsNew)
            row.Attributes.Add(className, "new");
        else if (row.Data.IsSelected)
            row.Attributes.Add(className, "active");
        else if (row.Attributes.ContainsKey(className))
            row.Attributes.Remove(className);
    }

    void LoadIdentityProvisioningLst(LoadDataArgs args)
    {
        var act = new SearchIdentityProvisioningAction { Filter = args.Filter, OrderBy = args.OrderBy, Skip = args.Skip, Take = args.Top };
        dispatcher.Dispatch(act);
    }
}